import { createRouter, createWebHistory } from "vue-router";
import home from '@/views/home.vue';
import { Toast } from "vant";
const routes = [
    {
        path: '/',
        redirect: '/home',
        meta: {
            keepAlive: true
        },
    },
    {
        path: '/home',
        name: 'home',
        meta: {
            keepAlive: true
        },
        component: home,
    },
    {
        path: '/friends',
        name: 'friends',
        meta: {
            keepAlive: true
        },
        component: () => import('@/views/friends.vue')
    },
    {
        path: '/select',
        name: 'select',
        meta: {
            keepAlive: true
        },
        component: () => import('@/views/select.vue')
    },
    {
        path: '/select/:id',
        name: 'carsPage',
        meta: {
            keepAlive: false
        },
        component: () => import('@/components/carsPage.vue')
    },
    {
        path: '/carDetail/threeCar',
        name: 'threeCar',
        meta: {
            keepAlive: false
        },
        component: () => import('@/components/threeCar.vue')
    },
    {
        path: '/carDetail/:id',
        name: 'carDetail',
        meta: {
            keepAlive: false
        },
        component: () => import('@/components/carDetail.vue')
    },
    {
        path: '/newsArticles/:id',
        name: "newsArticles",
        meta: {
            keepAlive: false
        },
        component: () => import('@/components/newsArticles.vue')
    },
    {
        path: '/wechatMon/:id',
        name: 'wechatMon',
        meta: {
            keepAlive: false
        },
        component: () => import('@/components/wechatMon.vue')
    },
    // {
    //     path: '/payCar',
    //     name: 'payCar',
    //     meta: {
    //         keepAlive: true
    //     },
    //     component: () => import('@/views/payCar.vue')
    // },
    {
        path: '/user',
        name: 'user',
        meta: {
            keepAlive: false
        },
        component: () => import('@/views/user.vue')
    },
    {
        path: '/user/userSet',
        name: 'userSet',
        meta: {
            keepAlive: false
        },
        component: () => import('@/components/userSet.vue')
    },
    {
        path: '/user/userSet/userConfig',
        name: 'userConfig',
        meta: {
            keepAlive: false
        },
        component: () => import('@/components/userConfig.vue')
    },
    {
        path: '/:catchMatch(.*)*',
        name: "NotFound",
        meta: {
            name: "NotFound",
            index: "NotFound"
        },
        component: () => import('@/components/notFound.vue')
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

export const include = routes.map((p) => {
    if (p.meta.keepAlive) { return p.name }
}).filter((p) => (p !== undefined))

export const jurisdictionRouter = [
    '/user/userSet',
    '/select'
]

router.beforeEach((to, from) => {
    if (jurisdictionRouter.some(item => item === to.fullPath)) {
        if (localStorage.getItem(import.meta.env.VITE_APP_TOKEN)) {
            return true
        } else {
            Toast.fail('请先登录')
            setTimeout(() => {
                router.push('/user')
            }, 1000)
            return false
        }
    }
    return true
})

export default router